//
//  AiRoleItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/4/1.
//

import SwiftUI
import Kingfisher

/**
 *  api 错误信息
 */
struct ApiCustomErrorItemView : View {
    
    var bean:AiCustomErrorBean
    @Binding var choose: Bool
    @State var select: Bool = false
    var body: some View {
        ZStack {
            contentView
                .contentShape(Rectangle())
                .onTapGesture {
                    if choose {
                        bean.select = !(bean.select ?? false)
                        select = bean.select ?? false
                    }
                }.onAppear{
                    select = bean.select ?? false
                }
        }
    }
    
    
}

//内容试图
extension ApiCustomErrorItemView {
    private var contentView: some View {
        ZStack(alignment:.topLeading) {
            VStack(spacing: 5) {
                Text(TimeUtils.getDate(time: bean.time ?? 0,format: "yyyy-MM-dd HH:mm"))
                    .font(.system(size: 16, weight: .medium))
                    .foregroundColor(.bgNormalText)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Text(highlightAndBoldText(fullText: "类型：\(bean.name ?? "")", boldTexts: ["类型："], size: 14))
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Text(highlightAndBoldText(fullText: "API地址：\(bean.host ?? "")", boldTexts: ["API地址："], size: 14))
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Text(highlightAndBoldText(fullText: "模型名称：\(bean.model ?? "")", boldTexts: ["模型名称："], size: 14))
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Text(highlightAndBoldText(fullText: "密钥(令牌)：\(bean.token ?? "")", boldTexts: ["密钥(令牌)："], size: 14))
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Text(highlightAndBoldText(fullText: "错误信息：", boldTexts: ["错误信息："], size: 14))
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
                Text("\(bean.content ?? "")")
                    .font(.system(size: 14))
                    .foregroundColor(.bgNormalText4)
                    .multilineTextAlignment(.leading)
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            HStack {
                Spacer()
                if choose == false {
                    Image(.appIconCopy)
                        .resizable()
                        .padding(5)
                        .frame(width: 30,height: 30)
                        .onTapGesture {
                            showToast("复制成功")
                            copyClipboard(bean.content)
                        }
                } else {
                    Image(select ? .appIcCheckboxSelect : .appIcCheckbox)
                        .resizable()
                        .renderingMode(.template)
                        .foregroundColor(themeManager.themeColor)
                        .frame(width: 20, height: 20)
                }
            }
        }.frame(maxWidth: .infinity)
            .padding(EdgeInsets(top: 15, leading: 15, bottom: 15, trailing: 10))
            .background(.bgItem)
            .cornerRadius(15)
    }
}

//定义预览提供者
struct ApiCustomErrorItemView_Previews: PreviewProvider {
    static var previews: some View {
        @State var choose : Bool = false
        configurePreviewEnvironment(for: ApiCustomErrorItemView(bean:AiCustomErrorBean(),choose: $choose))
    }
}
